/* To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package clientrmi;

import demoserverrmi.IServices;
import java.awt.image.BufferedImage;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Map;
import view.MainWindow;
import static view.MainWindow.man;



/**
 *
 * @author alulab11
 */
public class ClientRMI {

    /**
     * @param args the command line arguments
     */
    public static IServices proxy=null;
    public static Hilo hilo = null;
    public static Hilo hilo2 = null;
    public static MainWindow mw=null;
    public static void main(String[] args) {
     
        final Map map;
        try {
            Registry reg = LocateRegistry.getRegistry("192.168.0.30", 1099);
            proxy = (IServices)reg.lookup("MyServer");
            
          //  System.out.println("hola:      "+proxy.greetings("luisss"));
          //System.out.println("hola:      "+proxy.Cadena().getName());
             
            map=proxy.MapaRemoto();
          //  if(proxy.MapaRemoto()==null)
           //     System.out.println("Nulo");
            
            // System.out.println("   "+map.getCell(0, 0).getType());
           
             
            java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               mw = new MainWindow(map);
               mw.setVisible(true);
               hilo = new Hilo("Hilo");
               hilo.start();
               hilo2 = new Hilo("Hilo");
               hilo2.start();
            }
            });
            
    
      
               // MainWindow.man.setMap(proxy.MapaRemoto());
         
             //proxy.queryAllProducts();
        
    
        } catch (Exception ex) {
            Logger.getLogger(ClientRMI.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    

   
  
static class Hilo extends Thread {
public Hilo( String nombre ) {
super( nombre );
}

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(6000); //12000 o 11000 otro envia
                //proxy.EnivarMapaActualizado(mw.man.getMap());
                //Map map=proxy.MapaRemotoActualizado();
                 proxy.EnivarMapaActualizado(mw.man.getMap());
                //Cada cierto tiempo envio el mapa
                 
                 
                 
               // mw.canvas.repaint();
                System.out.println("hilo en accion");
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(ClientRMI.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }    }

}




static class Hilo2 extends Thread {
public Hilo2( String nombre ) {
super( nombre );
}

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(7000); //y recibe
                //proxy.EnivarMapaActualizado(mw.man.getMap());
                Map map=proxy.MapaRemotoActualizado();
                 //proxy.EnivarMapaActualizado(mw.man.getMap());
                //Cada cierto tiempo envio el mapa
                  mw.man.setMap(map);
                  mw.man.save("DB.xml");
                  mw.man.loadMap("DB.xml");
                 mw.mMap.repaint();
                  
               // mw.canvas.repaint();
                System.out.println("hilo en accion");
            } catch (InterruptedException ex) {
                ex.printStackTrace();
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            } catch (RemoteException ex) {
                Logger.getLogger(ClientRMI.class.getName()).log(Level.SEVERE, null, ex);
            }
           
        }    }

}

}